package com.glx.test.controller;

import com.glx.test.pojo.User;
import com.glx.test.pojo.UserInfos;
import com.glx.test.response.LoginResponse;
import com.glx.test.response.RegisterResponse;
import com.glx.test.response.RegisterUserInfosResponse;
import com.glx.test.response.UserInfosResponse;
import com.glx.test.service.Impl.UserServiceImpl;
import com.glx.test.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.Map;

@RestController

@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;


    @PostMapping("/register")
    public RegisterResponse register(@RequestBody User user)
    {
        System.out.println("进入register方法");
        System.out.println(user.toString());
        return userService.register(user);
    }

    @PostMapping("/registerUserInfos")
    public RegisterUserInfosResponse registerUserInfos(MultipartHttpServletRequest request){
        return userService.registerUserInfos(request);
    }

    @GetMapping("/login")
    public LoginResponse login(@RequestParam("account") String account,
                               @RequestParam("password") String password)
    {
        System.out.println("进入login方法");
        System.out.println(account+" "+password);

        return userService.login(account,password);
    }


    @GetMapping("/getUserInfos")
    public UserInfosResponse getUserInfos(@RequestParam("user_id") Long user_id,
                                          @RequestParam("token") String token)
    {
        System.out.println("进入getUserInfos方法");
        System.out.println(user_id+" "+" token:"+token);
        return userService.getUserInfos(user_id,token);
    }

    @PostMapping("/updateUserInfos")
    public UserInfosResponse updateUserInfos(@RequestBody UserInfos userInfos,
                                             @RequestParam("token") String token)
    {
        System.out.println("进入updateUserInfos方法");
        System.out.println(userInfos.toString()+" token:"+token);
        return userService.updateUserInfos(userInfos,token);
    }

    @PutMapping("/updateUserInfos_headpicture_url")
    public UserInfosResponse updateUserInfos_headpicture_url(MultipartHttpServletRequest request)
    {
        System.out.println("进入updateUserInfos_headpicture_url方法");
        return userService.updateUserInfos_headpicture_url(request);
    }

    @PostMapping("/verifytoken")
    public RegisterUserInfosResponse verifytoken(@RequestParam("token") String token)
    {
        RegisterUserInfosResponse registerUserInfosResponse=new RegisterUserInfosResponse();
        try {
            JwtUtils.verify(token);
        } catch (Exception e) {
            registerUserInfosResponse.setResult("fail");
            registerUserInfosResponse.setMessage("token验证失败");
            return registerUserInfosResponse;
        }
        registerUserInfosResponse.setResult("success");
        registerUserInfosResponse.setMessage("token验证成功");
        return registerUserInfosResponse;
    }

    @PutMapping("/updateUser_password")
    public UserInfosResponse updateUser_password(@RequestParam("user_id") Long user_id,
                                                @RequestParam("account") String user_account,
                                                 @RequestParam("old_password") String old_password,
                                                @RequestParam("user_password") String user_password,
                                                @RequestParam("token") String token)
    {
        System.out.println("进入updateUser_password方法");
        return userService.updateUser_password(user_id,user_account,old_password,user_password,token);
    }
}
